package com.demo.supplier.config;

import com.demo.supplier.constant.ApiConstants;
import com.demo.supplier.interceptor.AuthInterceptor;
import com.demo.supplier.interceptor.LogInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Web MVC配置
 * 
 * @author demo
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private AuthInterceptor authInterceptor;

    @Autowired
    private LogInterceptor logInterceptor;

    @Autowired
    private ApiConstants apiConstants;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 认证拦截器
        registry.addInterceptor(authInterceptor)
                .addPathPatterns(apiConstants.getApiPattern())
                .excludePathPatterns(
                        apiConstants.getLoginPath(),
                        apiConstants.getLogoutPath()
                );

        // 日志拦截器
        registry.addInterceptor(logInterceptor)
                .addPathPatterns(apiConstants.getApiPattern());
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // Knife4j静态资源
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        
        // Vue前端静态资源
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/");
    }

}